<template>
  <div class="main">
    <el-row type="flex" justify="space-between" align="middle" class="bt-row">
      <h3>Production Info</h3>
      <div>
        <el-button type="primary" icon="el-icon-plus" @click="addClick">New</el-button>
      </div>
    </el-row>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="name" label="Product Series" width="220">
      </el-table-column>
      <el-table-column prop="belong" label="Type" width="120">
      </el-table-column>
      <el-table-column prop="imgurl" label="Image" width="240">
        <template slot-scope="scope">
            <el-image 
              style="width:200px"
              :src="scope.row.imgurl"
              fit="contain"
            >
            </el-image>
        </template>  
      
      </el-table-column>
      <el-table-column label="Datasheet" width="240"> 
        <template slot-scope="scope">
            <el-link type="primary" :href="scope.row.datasheeturl" target="_blank">{{scope.row.datasheeturl}}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="brief" label="Breif"> </el-table-column>
      <el-table-column fixed="right" label="OP" width="60">
        <template slot-scope="scope">

          <el-button @click="handleClick(scope.row)" type="primary"  icon="el-icon-edit" circle size="small"  style="display:block; margin:5px 3px"
            ></el-button>
          <el-button @click="modelClick(scope.row)" type="primary" icon="el-icon-folder-opened" circle size="small" style="display:block; margin:5px 3px"
            ></el-button
          >
          <el-button @click="deleteClick(scope.row)" type="danger" icon="el-icon-delete" circle size="small"  style="display:block;margin:5px 3px"
            ></el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-row>

      <el-pagination
        layout="prev, pager, next"
        :total="total"
        :current-page.sync="currentPage"
        :page-size="pageSize"
        @current-change="pageClick"
        
        >
      </el-pagination>
      
    </el-row>

    <el-dialog title="Production Editor" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <!-- <el-form-item label="ID" :label-width="formLabelWidth">
          <el-input v-model="form.id" autocomplete="off" disabled></el-input>
        </el-form-item> -->
        <el-form-item label="Serie Name" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="Type" :label-width="formLabelWidth">
          <el-select v-model="form.belong" placeholder="Choose Type">
            <el-option label="TDK-Lambda" value="TDK-Lambda"></el-option>
            <el-option label="XP Power" value="XP Power"></el-option>
            <el-option label="Vicor" value="Vicor"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="Brief" :label-width="formLabelWidth">
          <el-input
            type="textarea"
            rows="5"
            v-model="form.brief"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="Image" :label-width="formLabelWidth">
          <el-button @click="getImgFile" style="margin-top: 10px">
            <i class="el-icon-upload"></i>CLICK_UPLOAD
          </el-button>
          {{ this.form.imgUrl }}
          <input
            type="file"
            ref="imgFile"
            style="display: none"
            @change="handleImg($event)"
            utocomplete="off"
          />
        </el-form-item>

        <el-form-item label="DataSheet" :label-width="formLabelWidth">
          <el-button @click="getSheetFile" style="margin-top: 10px">
            <i class="el-icon-upload"></i>CLICK_UPLOAD
          </el-button>
          {{ this.form.sheetUrl }}
          <input
            type="file"
            ref="sheetFile"
            style="display: none"
            @change="handleSheet($event)"
            utocomplete="off"
          />
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer" v-loading="loading">
        <el-button @click="dialogFormVisible = false">Cancel</el-button>
        <el-button type="primary" @click="confirmClick">OK</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getProducts,
  saveProduct,
  deleteProduct,
  deleteModel,
} from "@/api/production";

export default {
  data() {
    return {
      tableData: [],
      form: {
        id: "",
        name: "",
        belong: "",
        brief: "",
        imgUrl: "",
        sheetUrl:"",
        imgFile: null,
        sheetFile:null,
      },
      dialogFormVisible: false,
      formLabelWidth: "100px",
      loading: false,
      currentPage:1,
      total:0,
      pageSize:10,
    };
  },

  created() {
    this.getData();
  },

  methods: {
    getData() {
      getProducts(this.currentPage, this.pageSize).then((response) => {
        console.log(response);
        var records = response.data.records;
        this.tableData = records;

        this.currentPage = response.data.current;
        this.total = response.data.total
      });
    },

    handleClick(row) {
      this.dialogFormVisible = true;
      this.form = Object.assign({}, row);
      this.loading = false;
    },

    getImgFile() {
      this.$refs.imgFile.click();
    },

    getSheetFile() {
      this.$refs.sheetFile.click();
    },

    handleImg(event) {
      event.preventDefault();
      let file = this.$refs.imgFile.files[0];
      this.$set(this.form, "imgUrl", file.name);
      this.form.imgFile = file;
      console.log("select img file ", file);
    },

    handleSheet(event) {
      event.preventDefault();
      let file = this.$refs.sheetFile.files[0];
      this.$set(this.form, "sheetUrl", file.name);
      this.form.sheetFile = file;
      console.log("select sheet file ", file);
    },

    confirmClick() {
      this.loading = true;
      saveProduct(this.form).then((response) => {
        console.log(response);
        this.loading = false;
        this.loading = false;
        this.dialogFormVisible = false;
        this.$message.success("save OK");
        this.getData();
      });
    },
    addClick() {
      this.handleClick();
    },
    deleteClick(row) {
      this.$confirm("delete product?", "hint", {
        confirmButtonText: "OK",
        cancelButtonText: "cancel",
        type: "warning",
      }).then(() => {
        deleteProduct(row.id).then((res1) => {
          this.$message({
            type: "success",
            message: "delete record!",
          });
          this.getData();
        });
      });
    },
    modelClick(row) {
      this.$router.push({ name: "series", params: { name: row.name } });
    },
    pageClick(num){
      console.log(num)
      this.getData()
    }
  },
};
</script>

<style lang="scss">
.main {
  padding: 8px;
}
.bt-row {
  padding: 8px;
}
</style>